﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.Storage.Queue;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Azure.Deployment.Utils
{
    public static class Extensions
    {
        public static void PutValue(this CloudQueueMessage message, object value)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer s = new XmlSerializer(value.GetType());
                s.Serialize(sw, value);
                message.SetMessageContent(sw.ToString());
            }
        }

        public static T GetValue<T>(this CloudQueueMessage message)
        {
            using (StringReader ms = new StringReader(message.AsString))
            {
                XmlSerializer s = new XmlSerializer(typeof(T));
                return (T)s.Deserialize(ms);
            }
        }
    }
}
